<?php

/**
 * This is the model class for table "recoveryemails".
 *
 * The followings are the available columns in table 'recoveryemails':
 * @property integer $id
 * @property integer $user_id
 * @property string $uniqid
 * @property integer $created
 * @property integer $expired
 */
class RecoveryEmail extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return RecoveryEmail the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{recoveryemails}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, uniqid, expired', 'required'),
            array('user_id, created, expired', 'numerical', 'integerOnly' => true),
            array('uniqid', 'length', 'max' => 32),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, user_id, uniqid, created, expired', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // class name for the relations automatically generated below.
        return array(
            'user' => array(self::BELONGS_TO, 'User', 'user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'user_id' => 'User',
            'uniqid' => 'Uniqid',
            'created' => 'Create Time',
            'expired' => 'Exp Time',
        );
    }

    public function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->created = time();
            }

            return true;
        }

        return false;
    }

}